## summarize the MethyPatternAlignByBedWalker's data point result, count how many group of "1" cluster in one line
##e.g. it means for each CTCF site, how many NDRs around +/- 1kb region of CTCF motif center
## author: Yaping Liu  lyping1986@gmail.com

my $file=$ARGV[0];
my $scale=$ARGV[1];
my $use_age = "USAGE: perl sumFeature.pl input_file_name scale_to_look_at";
if($ARGV[0] eq "" or $ARGV[1] eq ""){
	print "$use_age\n";
	exit(1);
}
open(FH,"<$file") or die;
my $outfile=$file.".sumFeature.txt";
open(OUT,">$outfile") or die;
while(<FH>){
	chomp;
	my @splitin=split "\t";
	my $flag=0;
	my $sum=0;
	for(my $i=(4+(($#splitin-4)/2)-$scale);$i< (4+(($#splitin-4)/2)+$scale); $i++){
		if($splitin[$i] eq '1' and $flag==0){
			$sum++;
			$flag=1;
		}
		elsif($splitin[$i] ne '1' and $flag==1){
			$flag=0;
		}
	}
	print OUT "$sum\n";
}
close(FH);


close(OUT);